﻿using QQMusic.API;
using QQMusic.Model;
using QQMusic.ViewModel.Base;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace QQMusic.ViewModel
{
    public class MainWindowViewModel : ViewModelBase
    {
        #region 事件

        /// <summary>
        /// 最小化
        /// </summary>
        public ICommand OnMinClickCommand => new DelegateCommand((sender) =>
        {
            if (sender is MainWindow window)
            {
                window.WindowState = WindowState.Minimized;
            }
        });

        /// <summary>
        /// 最大化
        /// </summary>
        public ICommand OnMaxClickCommand => new DelegateCommand((sender) =>
        {
            if (sender is MainWindow window)
            {
                if (window.WindowState == WindowState.Maximized)
                {
                    window.WindowState = WindowState.Normal;
                }
                else if (window.WindowState == WindowState.Normal)
                {
                    window.WindowState = WindowState.Maximized;
                }
            }
        });

        /// <summary>
        /// 关闭
        /// </summary>
        public ICommand OnCloseClickCommand => new DelegateCommand((sender) =>
        {
            if (sender is MainWindow window)
            {
                window.Hide();
                window.Close();
            }
        });

        public ParamsCommand<TextBox> MouseDownCommand => new ParamsCommand<TextBox>((textbox) =>
        {
            this.SearchVisibility = Visibility.Collapsed;
            this.TextBoxVisibility = Visibility.Visible;
            if (textbox != null && textbox is TextBox)
            {
                textbox.Focus();
            }
            SearchPopupShow = true;
        });

        public ParamsCommand<Popup> LostFocusCommand => new ParamsCommand<Popup>((popup) =>
        {
            if (keyword == "")
            {
                this.TextBoxVisibility = Visibility.Collapsed;
                this.SearchVisibility = Visibility.Visible;
            }
            SearchPopupShow = false;
        });

        public ICommand MouseEnterCommand => new DelegateCommand((sender) =>
        {
            this.TimesVisibility = Visibility.Collapsed;
            this.TypesVisibility = Visibility.Visible;
        });

        public ICommand MouseLeaveCommand => new DelegateCommand((sender) =>
        {
            this.TypesVisibility = Visibility.Collapsed;
            this.TimesVisibility = Visibility.Visible;
        });

        public ICommand PlusClickCommand => new DelegateCommand((sender) =>
        {

        });

        #endregion

        #region 属性
        private Page currentPage = Pages.Home;

        /// <summary>
        /// 当前页面
        /// </summary>
        public Page CurrentPage
        {
            get { return currentPage; }
            set
            {
                currentPage = value;
                OnPropertyChanged(nameof(CurrentPage));
            }
        }

        private Menus selectMenu;

        /// <summary>
        /// 选中菜单
        /// </summary>
        public Menus SelectMenu
        {
            get { return selectMenu; }
            set
            {
                selectMenu = value;
                switch (selectMenu)
                {

                    case Menus.Home: CurrentPage = Pages.Home; break;

                    case Menus.Video: CurrentPage = Pages.Video; break;

                    case Menus.Radio: CurrentPage = Pages.Radio; break;

                    case Menus.ILike: CurrentPage = Pages.ILike; break;

                    case Menus.Local: CurrentPage = Pages.Local; break;

                    case Menus.History: CurrentPage = Pages.History; break;

                    case Menus.Lists: CurrentPage = Pages.Lists; break;

                    case Menus.Create: CurrentPage = Pages.Create; break;

                    case Menus.Collect: CurrentPage = Pages.Collect; break;

                    default: CurrentPage = Pages.Home; break;

                }
                OnPropertyChanged(nameof(SelectMenu));
            }
        }

        private Visibility searchVisibility = Visibility.Visible;

        public Visibility SearchVisibility
        {
            get => searchVisibility;
            set
            {
                searchVisibility = value;
                OnPropertyChanged(nameof(SearchVisibility));
            }
        }

        private Visibility textBoxVisibility = Visibility.Collapsed;

        public Visibility TextBoxVisibility
        {
            get => textBoxVisibility;
            set
            {
                textBoxVisibility = value;
                OnPropertyChanged(nameof(TextBoxVisibility));
            }
        }

        private Visibility timesVisibility = Visibility.Visible;

        public Visibility TimesVisibility
        {
            get => timesVisibility;
            set
            {
                timesVisibility = value;
                OnPropertyChanged(nameof(TimesVisibility));
            }
        }

        private Visibility typesVisibility = Visibility.Collapsed;

        public Visibility TypesVisibility
        {
            get => typesVisibility;
            set
            {
                typesVisibility = value;
                OnPropertyChanged(nameof(TypesVisibility));
            }
        }

        private string keyword = "";

        public string Keyword
        {
            get => keyword;
            set
            {
                keyword = value;
                OnPropertyChanged(nameof(Keyword));
            }
        }

        private bool searchPopupShow = false;

        public bool SearchPopupShow
        {
            get => searchPopupShow;
            set
            {
                searchPopupShow = value;
                OnPropertyChanged(nameof(SearchPopupShow));
            }
        }

        private readonly static Collection<Search> SearcheSource = new Collection<Search>()
        {
            new Search{ Id=1, Keyword = "白月光与朱砂痣", Plays = "2614万" },
            new Search{ Id=2, Keyword = "踏山河", Plays = "705万" },
            new Search{ Id=3, Keyword = "简单的幸福", Plays = "703万" },
            new Search{ Id=4, Keyword = "四季予你", Plays = "595万" },
            new Search{ Id=5, Keyword = "醒不来的梦", Plays = "409万" },
            new Search{ Id=6, Keyword = "花间酒", Plays = "332万" },
            new Search{ Id=7, Keyword = "许嵩", Plays = "323万" },
            new Search{ Id=8, Keyword = "浪子闲话", Plays = "292万" },
            new Search{ Id=9, Keyword = "蔡徐坤", Plays = "280万" },
            new Search{ Id=10, Keyword = "林俊杰", Plays = "254万" }
        };

        private ObservableCollection<Search> searches = new ObservableCollection<Search>(SearcheSource);
        
        public ObservableCollection<Search> Searches
        {
            get => searches;
            set
            {
                searches = value;
                OnPropertyChanged(nameof(Searches));
            }
        }

        #endregion

    }
}
